#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define cvt(i) (b + i)

const int N = 5e3 + 5, inf = 0x3f3f3f3f;
int a, b, c, cnt, d, ans, lim, tot, h[N], s, t;

struct Node {
  int x, y, z;
} node[N];

int e_cnt = 1, heads[N], cur[N];

struct Edge {
  int v, nxt, f;
} e[N << 1];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].f = w;
  e[++e_cnt].v = u, e[e_cnt].nxt = heads[v], heads[v] = e_cnt, e[e_cnt].f = 0;
}

int q[N], l, r;

inline bool bfs() {
  q[l = r = 1] = 0;
  for (int i = 1; i <= t; ++i) h[i] = 0, cur[i] = heads[i];
  h[0] = 1, cur[0] = heads[0];
  while (l <= r) {
    int u = q[l++];
    for (int i = heads[u], v; i; i = e[i].nxt) {
      if (!h[v = e[i].v] && e[i].f) {
        h[v] = h[u] + 1;
        if (v == t) return true;
        q[++r] = v;
      }
    }
  }
  return false;
}

int dinic(int u, int flow) {
  if (u == t) return flow;
  int rest = flow, k;
  for (int i = cur[u], v; i && rest; i = e[i].nxt) {
    if (h[v = e[i].v] == h[u] + 1 && e[i].f) {
      k = dinic(v, std::min(rest, e[i].f));
      if (!k) h[v] = 0;
      e[i].f -= k;
      e[i ^ 1].f += k;
      rest -= k;
      if (!rest) {
        cur[u] = i;
        return flow - rest;
      }
    }
  }
  cur[u] = 0;
  return flow - rest;
}

inline void solve(int sta) {
  int tmp = 0;
  e_cnt = 1;
  t = b + c + 1;
  heads[0] = heads[t] = 0;
  for (int i = 1; i <= b; ++i) heads[i] = 0;
  for (int i = 1; i <= c; ++i) heads[cvt(i)] = 0;

  for (int i = 0; i < a; ++i)
    if (!((sta >> i) & 1)) ++tmp;

  for (int i = 1; i <= cnt; ++i) {
    if ((sta >> (node[i].x - 1)) & 1) { add(node[i].y, cvt(node[i].z), 1); }
  }
  for (int i = 1; i <= b; ++i) add(0, i, 1);
  for (int i = 1; i <= c; ++i) add(cvt(i), t, 1);
  int flow;
  while (bfs()) {
    if ((flow = dinic(0, inf))) tmp += flow;
  }
  ans = std::min(ans, tmp);
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("P3231 [HNOI2013]消毒.in", "r", stdin);
  freopen("P3231 [HNOI2013]消毒.out", "w", stdout);
#endif
#endif

  scanf("%d", &d);
  while (d--) {
    scanf("%d%d%d", &a, &b, &c);
    cnt = 0;
    for (int i = 1, tmp; i <= a; ++i) {
      for (int j = 1; j <= b; ++j) {
        for (int k = 1; k <= c; ++k) {
          scanf("%d", &tmp);
          if (tmp) { node[++cnt].x = i, node[cnt].y = j, node[cnt].z = k; }
        }
      }
    }
    lim = std::min(a, std::min(b, c));
    if (a != lim && b == lim) {
      std::swap(a, b);
      for (int i = 1; i <= cnt; ++i) std::swap(node[i].x, node[i].y);
    } else if (c == lim) {
      std::swap(a, c);
      for (int i = 1; i <= cnt; ++i) std::swap(node[i].x, node[i].z);
    }
    ans = inf;
    lim = 1 << a;
    for (int i = 0; i < lim; ++i) { solve(i); }
    printf("%d\n", ans);
  }
  return 0;
}